// pages/onlineUnderway/onlineUnderway.js
const app = getApp()
import api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetailInfo:{},
    showLogin: false,
    card: {},
    imgUrl: '',

    imgs:[],
    isUpload: false,
    canAdd:true
  },
  showModal() {
    let obj = {}
    obj.order_identifier = this.data.orderDetailInfo.order_identifier
    console.log(obj.order_identifier)
    api.POST('/applet/exam_order/checkExamPermit', obj, res => {
      // 准考证
      console.log(res.data)
      this.setData({
        card: res.data
      })
    })

    api.POST('/applet/ContactService/detail', {}, res => {
      console.log(res.data.exam_card_url)
      this.setData({
        imgUrl: res.data.exam_card_url
      })
    })
    this.setData({
      showLogin: true
    })
  },
  close() {
    this.setData({
      showLogin: false
    })
  },

  addImg(){
    const that = this;
    const token = wx.getStorageSync('token');
    // let count = 9 - this.data.imgs.length;
    wx.chooseImage({
      // count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        wx.showLoading({
          title: '加载中',
        })
        let l = tempFilePaths.length;
        tempFilePaths.forEach((item, index) => {
          wx.uploadFile({
            url: api.API_URL + '/applet/upload/image',
            filePath: item,
            name: 'files',
            formData: {
              'session_id': token
            },
            success: function (res) {
              console.log(res)
              let obj = JSON.parse(res.data);
              let arr = that.data.imgs;
              arr.push(obj.data);
              that.setData({
                imgs: arr
              })
              if (index + 1 == l) {
                wx.hideLoading();
              }
            }
          })
        })

      }
    })
  },
  delImg(e){
    let idx = e.currentTarget.dataset.idx
    let arr = this.data.imgs
    arr.splice(idx,1)
    this.setData({
      imgs:arr,
    })
  },
  uploadImg(){
    api.POST('/applet/exam_order/uploadExamImages',{
      "exam_order_id": this.data.orderDetailInfo.exam_order_id,
      "images_url":this.data.imgs
    },res => {
      console.log(this.data.imgs)
      this.setData({
        
        isUpload:true,
        canAdd: false
      })
      wx.showToast({
        title: '上传成功',
      })

      api.POST('/applet/exam_order/detail', {
        exam_order_id: this.data.exam_order_id
      }, res => {
        this.setData({
          imgs: res.data.exam_images
        })
      })
    })
  },

  savePermit() {
    let that = this
    wx.setClipboardData({
      data: this.data.card.card_identifier,
      success() {
        wx.getClipboardData({
          success(res) {
            wx.showToast({
              title: '保存准考证成功',
            })
            that.close()
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      exam_order_id: options.oid
    })
    console.log(this.data.exam_order_id)
    api.POST('/applet/exam_order/detail', {
      exam_order_id: options.oid
    }, res => {
      console.log(res)
      console.log(res.data.exam_status, res.data.status)
      this.setData({
        orderDetailInfo: res.data
      })
      if (this.data.orderDetailInfo.exam_images.length != 0) {
        this.setData({
          isUpload: true,
          canAdd: false,
          imgs: res.data.exam_images
        })
      }
    })

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})